//
//  MyBankCardViewController.m
//  Findif
//
//  Created by Mac on 2018/11/7.
//  Copyright © 2018年 Cicel Chen. All rights reserved.
//

#import "MyBankCardViewController.h"
#import "AddBankViewController.h"
#import "BankTableViewCell.h"
#import "BankModel.h"

@interface MyBankCardViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, weak)UIImageView * backImageview;
@property (nonatomic, weak)UILabel * exploreLabel;
@property (nonatomic, weak)UITableView * tableView;
@property (nonatomic, strong)NSArray <BankModel *> * bankArray;

@end

@implementation MyBankCardViewController

- (void)viewWillAppear:(BOOL)animated{
    
    [self loadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = ZRWhiteColor;
//    [self loadData];
    [self setupNav];
    [self setupUI];
}


- (void)setBankArray:(NSArray<BankModel *> *)bankArray{
    
    _bankArray = bankArray;
    
    [self.tableView reloadData];
}


- (void)loadData{
    
    [[ZRNetworkTool sharedTool] getBingBankListsResultWithBlock:^(BOOL success, id responseData) {
        
        if (success) {
            
            self.bankArray = [NSArray yy_modelArrayWithClass:[BankModel class] json:responseData[@"data"]];
        }
    }];
}


- (void)setupNav{
    
    self.title = @"我的银行卡";
    
//    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back(1)"] style:UIBarButtonItemStylePlain target:nil action:nil];
//    
//    self.navigationItem.backBarButtonItem = backItem;
    
    
    UIButton * rightButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 20 * SCREENH6, 20 * SCREENH6)];
    
    rightButton.tag = 2;
    [rightButton setImage:[UIImage imageNamed:@"add"] forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(didNavButton:) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem * rightBar = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    
    self.navigationItem.rightBarButtonItem = rightBar;
}


- (void)setupUI{
    
    UITableView * tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.backgroundColor = ZRWhiteColor;
    tableView.rowHeight = 125 * SCREENH6;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.showsVerticalScrollIndicator = NO;
    
    self.tableView = tableView;
    
    
    [self.tableView registerClass:[BankTableViewCell class] forCellReuseIdentifier:@"BankTableViewCell"];
    
    [self.view addSubview:self.tableView];
    
    UIImageView * backImageView = [UIImageView new];
    
    backImageView.image = [UIImage imageNamed:@"无银行卡"];
    
    self.backImageview = backImageView;
    
    [self.view addSubview:self.backImageview];
    
    
    UILabel * exploreLabel = [UILabel new];
    
    exploreLabel.textColor = [UIColor lightGrayColor];
    exploreLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    exploreLabel.text = @"亲，你还没有绑定银行卡哦！\n赶快绑定银行卡吧！";
    exploreLabel.textAlignment = NSTextAlignmentCenter;
    exploreLabel.numberOfLines = 2;
    
    self.exploreLabel = exploreLabel;
    
    [self.view addSubview:self.exploreLabel];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (self.bankArray.count == 0) {
        
        self.backImageview.hidden = NO;
        self.exploreLabel.hidden = NO;
    }else{
        
        self.backImageview.hidden = YES;
        self.exploreLabel.hidden = YES;
    }
    
    return self.bankArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    BankTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BankTableViewCell" forIndexPath:indexPath];
    
    cell.model = self.bankArray[indexPath.row];
    
    return cell;
}


- (void)viewDidLayoutSubviews{
    
    [self.backImageview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.top.mas_equalTo(self.view).offset(155 * SCREENH6);
        make.width.mas_equalTo(175 * SCREENH6);
        make.height.mas_equalTo(144 * SCREENH6);
    }];
    
    [self.exploreLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.top.mas_equalTo(self.backImageview.mas_bottom).offset(20 * SCREENH6);
    }];
}

- (void)didNavButton:(UIButton *)button{
    
        
        HiddenBar;
        
        AddBankViewController * addVC = [AddBankViewController new];
        
        [self.navigationController pushViewController:addVC animated:YES];
}

@end
